import axios from 'axios'
import VueCookies from 'vue-cookies'
const instance = axios.create({
  baseURL: _config.baseUrl
});

export default new class {
  //post请求
  async post(url, data) {
    var res = await instance.post(url, data,{
      headers: {
        Authorization: `Bearer ${VueCookies.get('access_token')}`
      }
    });
    return res.data;
  }
  async get(url) {
    var res = await instance.get(encodeURI(url),{
      headers: {
        Authorization: `Bearer ${VueCookies.get('access_token')}`
      }
    });
    return res.data;
  }
  async getByToken(url, token) {
    var res = await instance.get(encodeURI(url), {
      headers: {
        Authorization: `Bearer ${token}`
      }
    });
    return res.data;
  }
  async delete(url, data) {
    var res = await instance.delete(url, {
      data: data,
      headers: {
        Authorization: `Bearer ${VueCookies.get('access_token')}`
      }
    });
    return res.data;
  }
  async put(url, data) {
    var res = await instance.put(url, data,{
      headers: {
        Authorization: `Bearer ${VueCookies.get('access_token')}`
      }
    });
    return res.data;
  }
  //上传文件
  async uploadFile(url, flie) {
    const formData = new FormData();
    formData.append("file", flie);
    var res = await instance.put(url, formData, {
      'Content-type': 'multipart/form-data',
      headers: {
        Authorization: `Bearer ${VueCookies.get('access_token')}`
      }
    });
    return res.data;
  }
}
